package net.ewon.avatar.pkgmgmt.manager;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageFile;
import net.ewon.avatar.common.pkg.Version;

import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/AvatarPackageMgmt-beans.xml")
@Category(net.ewon.test.LinuxTests.class)
public class AvatarPackageManagerIT {

	@Resource(name = "avatarPackageManager")
	private AvatarPackageManager manager;

	private AvatarPackage pkg1;
	private AvatarPackage pkg2;

	private AvatarPackageFile pkgFile;

	@Before
	public void setUp() throws Exception {
		createAvatarPackage1();
		createAvatarPackage2();
	}

	@After
	public void tearDown() throws Exception {

		manager.removePackage(pkg1.getId());
		manager.removePackage(pkg2.getId());

		if (pkgFile != null) {
			manager.removePackageFile(pkgFile.getId());
		}
	}

	@Test
	public void testStore() {
		manager.store(pkg1);
		AvatarPackage pkg = manager.getAvatarPackage(pkg1.getName(),
				pkg1.getVersion());
		assertEquals(pkg1.getCreatedBy(), pkg.getCreatedBy());
		assertEquals(2, pkg.getFileIds().size());

		manager.store(pkg2);
		pkg = manager.getAvatarPackage(pkg2.getName(), pkg2.getVersion());
		assertEquals(pkg2.getCreatedBy(), pkg.getCreatedBy());
		assertEquals(2, pkg.getFileIds().size());
	}

	@Test
	public void testGetAvatarPackageByName() {
		manager.store(pkg1);
		manager.store(pkg2);
		 
		List<AvatarPackage> pkgs = manager.getAvatarPackages(pkg1.getName());
		assertEquals(2, pkgs.size());
	}
 
	@Test
	public void testStorePackageFile() throws IOException {
		InputStream fis = this.getClass().getClassLoader()
				.getResourceAsStream("AvatarPackageMgmt-beans.xml");

		pkgFile = new AvatarPackageFile(
				"AvatarPackageMgmt/1.0.0/AvatarPackageMgmt-beans.xml",
				IOUtils.toByteArray(fis));

		manager.storeAvatarPackageFile(pkgFile);

		AvatarPackageFile file = manager.getAvatarPackageFile(pkgFile.getId());

		assertTrue(file.getData().length > 0);
	}

	private void createAvatarPackage1() {
		List<String> files = new ArrayList<String>();
		files.add("AvatarPackageMgmt/1.0.0/AvatarPackageMgmtService/index.jsp");
		files.add("AvatarPackageMgmt/1.0.0/AvatarPackageMgmtService/WEB-INF/lib/AvatarPackageMgmt.jar");

		pkg1 = new AvatarPackage("AvatarPackageMgmt", new Version(1, 0, 0),
				files, "guangzh", new Date(System.currentTimeMillis()));
	}

	private void createAvatarPackage2() {
		List<String> files = new ArrayList<String>();
		files.add("AvatarPackageMgmt/1.1.0/AvatarPackageMgmtService/index.jsp");
		files.add("AvatarPackageMgmt/1.1.0/AvatarPackageMgmtService/WEB-INF/lib/AvatarPackageMgmt.jar");

		pkg2 = new AvatarPackage("AvatarPackageMgmt", new Version(1, 1, 0),
				files, "guangzh", new Date(System.currentTimeMillis()));
	}

}
